package com.tt.core.cassandra.settings;

import java.util.Arrays;

import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.service.ThriftKsDef;
import me.prettyprint.cassandra.service.template.ColumnFamilyResult;
import me.prettyprint.cassandra.service.template.ColumnFamilyTemplate;
import me.prettyprint.cassandra.service.template.ColumnFamilyUpdater;
import me.prettyprint.cassandra.service.template.ThriftColumnFamilyTemplate;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.ddl.ColumnFamilyDefinition;
import me.prettyprint.hector.api.ddl.ComparatorType;
import me.prettyprint.hector.api.ddl.KeyspaceDefinition;
import me.prettyprint.hector.api.exceptions.HectorException;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.query.SliceQuery;

public class Configurator {

	final private String host = "localhost";
	final private String port = "9160";
	final private String keyspace = "nosql";

	public void config() {

		Cluster cluster = HFactory.getOrCreateCluster("cluster", host + ":"
				+ port);

		Keyspace ksp = HFactory.createKeyspace(keyspace, cluster);

		ColumnFamilyDefinition cfDef = HFactory.createColumnFamilyDefinition(
				keyspace, "ColumnFamilyName", ComparatorType.BYTESTYPE);

		KeyspaceDefinition newKeyspace = HFactory.createKeyspaceDefinition(
				keyspace, ThriftKsDef.DEF_STRATEGY_CLASS, 1,
				Arrays.asList(cfDef));

		// Add the schema to the cluster.
		// "true" as the second param means that Hector will block until all
		// nodes see the change.
		cluster.addKeyspace(newKeyspace, true);

		ColumnFamilyTemplate<String, String> template = new ThriftColumnFamilyTemplate<String, String>(
				ksp, "Coluna", StringSerializer.get(), StringSerializer.get());

		// Updater
		ColumnFamilyUpdater<String, String> updater = template
				.createUpdater("a key");
		updater.setString("domain", "www.datastax.com");
		updater.setLong("time", System.currentTimeMillis());

		try {
			template.update(updater);
		} catch (HectorException e) {
			e.printStackTrace();
		}

		try {
			ColumnFamilyResult<String, String> res = template
					.queryColumns("a key");
			String value = res.getString("domain");

			System.out.println(value);
			// value should be "www.datastax.com" as per our previous insertion.
		} catch (HectorException e) {
			e.printStackTrace();
		}

		// Iterates over all columns for the row identified by key "a key"
		SliceQuery<String, String, String> query = HFactory
				.createSliceQuery(ksp, StringSerializer.get(),
						StringSerializer.get(), StringSerializer.get())
				.setKey("a key").setColumnFamily("Coluna");

	}
}